home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 462 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: solon.com!not-for-mail
  2. From: ua302aa@sun2.lrz-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c,comp.lang.c.moderated
  4. Subject: Re: Leading and Trailing Blanks
  5. Date: 5 Jan 1996 09:52:28 -0600
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4cjhfs$f6r@solutions.solon.com>
  10. References: <4chh1b$685@solutions.solon.com>
  11. NNTP-Posting-Host: solutions.solon.com
  12.  
  13.  
  14. mskc@io.com (Casey Claiborne) writes:
  15.  
  16.  
  17. >Hello -
  18. >    I am wondering if anyone out there has a program (or knows of one)
  19. >that allows one to strip leading and trailing blanks from a string. 
  20. >ex:
  21.  
  22. >    char test[20];
  23. >    strcpy(test,"  TESTING  ");
  24. >    printf("%s", test);
  25.  
  26. >will produce an output like
  27. >  TESTING  
  28.  
  29. >that has blanks at the beginning of "TESTING". I would like to 
  30. >have the following result
  31.  
  32. >TESTING
  33.  
  34. >that has no leading blanks.
  35.  
  36. >I would *greatly* appreciate any type of help or hints in working with
  37. >this.
  38.  
  39. Try something like this:
  40.  
  41.  
  42.        #include <ctype.h>
  43.        #include <string.h>
  44.  
  45.        char test[BIG_ENOUGH], *beg, *end;
  46.  
  47.        beg = "    TESTING     ";
  48.  
  49.        /* Skip leading white space characters */
  50.  
  51.        while(isspace(*beg))
  52.       ++beg; 
  53.  
  54.        strcpy(test, beg);
  55.  
  56.        /* Remove trailing blanks */
  57.  
  58.        end = strrchr(test, '\0');
  59.        if (end > test)
  60.        {
  61.       --end;
  62.       while(isspace(*end))
  63.          --end;
  64.           end[1] = '\0';
  65.        }
  66.  
  67. Kurt
  68. --
  69. | Kurt Watzka                             Phone : +49-89-2180-6254
  70. | watzka@stat.uni-muenchen.de
  71. | ua302aa@sunmail.lrz-muenchen.de
  72.